package com.kangzhili.aicoder.ai;

import com.kangzhili.aicoder.ai.tools.InterviewQuestionTool;
import dev.langchain4j.mcp.McpToolProvider;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.service.AiServices;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AiCodeServiceFactory {
    @Resource
    private ChatModel qwenChatModel;

    @Resource
    private ContentRetriever contentRetriever;

    @Resource
    private McpToolProvider mcpToolProvider;

    // 流式响应（打字机效果）
    @Resource
    private StreamingChatModel qwenStreamingChatModel;

    @Bean
    public AiCodeService aiCodeService() {
        // 会话记忆
        ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);
        // 构造AI Service
        AiCodeService aiCodeService = AiServices.builder(AiCodeService.class)
                .chatModel(qwenChatModel)
                // 流式响应（打字机效果）
                .streamingChatModel(qwenStreamingChatModel)
                .chatMemory(chatMemory)
                // 每个会话独立存储
                .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))
                // RAG 检索增强生成
                .contentRetriever(contentRetriever)
                // 工具调用
                .tools(new InterviewQuestionTool())
                // MCP 工具调用
                .toolProvider(mcpToolProvider)
                .build();
        return aiCodeService;
    }
}
